﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FBProject.BusinessLogic;
using System.Diagnostics;
using FBProject.BusinessLogic.MSProjectProxies;
using System.Reflection;

namespace FBProject
{
    public partial class FTaskDifferences : Form
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(FTaskDifferences));

        private IList<TaskDifference> TaskDifferences { get; set; }

        public FTaskDifferences(IList<TaskDifference> taskDifferences)
        {
            InitializeComponent();

            this.Text = ApplicationHelper.GetApplicationNameAndVersion(Assembly.GetExecutingAssembly().GetName()) + " - Task Differences Report";

            TaskDifferences = taskDifferences;
        }

        private void FTaskDifferences_Load(object sender, EventArgs e)
        {
            TaskDifferencesDataGridView.DataSource = new SortableBindingList<TaskDifference>(TaskDifferences);
        }

        private void TaskDifferencesDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
            {
                if (TaskDifferencesDataGridView.Columns[e.ColumnIndex] == TaskIDLinkColumn)
                {
                    var externalValue = TaskDifferencesDataGridView.Rows[e.RowIndex].Cells[externalValueDataGridViewTextBoxColumn.Name].Value;
                    if (externalValue != null)
                    {
                        Clipboard.SetText((string)externalValue);
                    }
                    GoToTask(
                        (int)TaskDifferencesDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value,
                        (string)TaskDifferencesDataGridView.Rows[e.RowIndex].Cells[MSProjectColumnNameDataGridViewTextBoxColumn.Name].Value);
                }
                else if (TaskDifferencesDataGridView.Columns[e.ColumnIndex] == FogBugzCaseLinkColumn)
                {
                    var msProjectValue = TaskDifferencesDataGridView.Rows[e.RowIndex].Cells[mSProjectValueDataGridViewTextBoxColumn.Name].Value;
                    if (msProjectValue != null)
                    {
                        Clipboard.SetText((string)msProjectValue);
                    }
                    OpenUrl((string)TaskDifferencesDataGridView.Rows[e.RowIndex].Cells[FBCaseUrlDataGridViewTextBoxColumn.Name].Value);
                }
            }
        }

        private void GoToTask(int id, string column)
        {
            var msProjectProxy = new MSProjectProxy(MSProjectApplicationProxy.GetMSProjectApplication());
            if (msProjectProxy.GoToTaskColumn(id, column))
            {
                msProjectProxy.Activate();
            }
            else
            {
                MessageBox.Show(string.Format("Can't find task with ID {0}. The task could be filtered out or hidden under collapsed parent.", id), "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void OpenUrl(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                try
                {
                    Process.Start(url);
                }
                catch (Exception ex)
                {
                    var message = string.Format("Cannot open url {0}", url);
                    Logger.Warn(message, ex);
                    MessageBox.Show(message, "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
